﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;

public partial class page_GoodType_Manager : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        // 记录SiteID,UltraWebGrid控件分页时_Site.ID的值会丢失。
        if (_Site != null)
        {
            txtSiteID.Text = _Site.ID.ToString();            
        }

        this.RegisterJS();

        if (!IsPostBack)
        {
            Utility.InitUltraWebGrid.InitDisplayLayOut(this.UWGGoodType);

            BindData("");
            LoadInitTree();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页
        //RequestCompetences = Competences.BuildCompetencesList(Competences.LBGL_70);//页面权限

        base.OnLoad(e);
    }

    #endregion
    
    protected void TVGoodType_SelectedNodeChanged(object sender, EventArgs e)
    {
        string ID = TVGoodType.SelectedNode.Value.ToString();                   //商品类别编号
        BindData(ID);                                                   //重新梆定商品类别
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("GoodType_Edit.aspx?Action=Add"); 
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            Response.Redirect("GoodType_Edit.aspx?Action=Edit&ID="+ID);
        }
    }

    protected void btnStop_Click(object sender, EventArgs e)
    {        
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            string TypeStatus = UWGGoodType.DisplayLayout.ActiveRow.Cells.FromKey("TypeStatus").Value.ToString();
            if (TypeStatus.Equals("停用"))
            {
                Shove._Web.JavaScript.Alert(this.Page, "该类别已经停用！");                
            }
            else
            {
                DAL.Tables.tbl_Shop_GoodType table = new DAL.Tables.tbl_Shop_GoodType();
                table.TypeStatus.Value = 0;
                table.Update("[ID]='" + Hidden1.Value + "'"); 
                Hidden1.Value = "";
                this.BindData("");
            }     
        }  
    }

    protected void btnResume_Click(object sender, EventArgs e)
    {        
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            string TypeStatus = UWGGoodType.DisplayLayout.ActiveRow.Cells.FromKey("TypeStatus").Value.ToString();
            if (TypeStatus.Equals("正常"))
            {
                Shove._Web.JavaScript.Alert(this.Page, "该类别已外于正常状态！");
            }
            else
            {
                DAL.Tables.tbl_Shop_GoodType table = new DAL.Tables.tbl_Shop_GoodType();
                table.TypeStatus.Value = 1;
                table.Update("[ID]='" + ID + "'");

                Hidden1.Value = "";
                this.BindData("");
            }
        }   
    }

    protected void btnExporterExcel_Click(object sender, EventArgs e)
    {
        Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "GoodType_Manager.xls");
    }

    protected void UWGGoodType_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        this.BindData("");
    }

    /// <summary>
    /// 加载商品类别信息
    /// </summary>
    private void BindData(string ID)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }
        long SiteID = Shove._Convert.StrToLong(txtSiteID.Text, -1);

        string Fields = "ID,TypeName,";
        Fields += "(case when TypeStatus ='1' then '正常' else '停用' end )as TypeStatus,ParentID,Introduce";
        string Condition = "(ShopID=" + SiteID + ")";
        string Sort = "";

        if ((!ID.Equals("")) && (!ID.Equals("00")))
        {
            Condition = "(ShopID=" + SiteID + ")  and (substring(ID,1,len('" + ID + "')))='" + ID + "'";
        }

        dt = new DAL.Tables.tbl_Shop_GoodType().Open(Fields, Condition, Sort);

        UWGGoodType.DataSource = dt;
        UWGGoodType.DataBind();
    }

    /// <summary>
    /// 加载商品类别信息到树中
    /// </summary>
    private void LoadInitTree()
    {
        string ID = "";

        string Fields = "ID,TypeName,";
        Fields += "(case when TypeStatus ='1' then '正常' else '停用' end )as TypeStatus,ParentID,Introduce";
        string Condition = "(ShopID=" + _Site.ID + ")";
        string Sort = "";

        if (!ID.Equals(""))
        {
            Condition = "(ShopID=" + _Site.ID + ")  and (substring(ID,1,len('" + ID + "')))='" + ID + "'";
        }

        dt = new DAL.Tables.tbl_Shop_GoodType().Open(Fields, Condition, Sort);

        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID='00'";    
        
        //添加一根节点
        TreeNode node1 = new TreeNode();
        node1.Text = "所有类别";
        node1.Value = "00";
        TVGoodType.Nodes.Add(node1);

        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = drv["TypeName"].ToString();
            node.Value = drv["ID"].ToString();
            node.Expanded = false;
            node1.ChildNodes.Add(node);
            AddReplies(dt, node);
        }
    }

    /// <summary>
    /// 递归绑定商品类别到树中 
    /// </summary>
    private void AddReplies(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID='" + node.Value + "'";

        foreach (DataRowView row in dv)
        {
            TreeNode replyNode = new TreeNode();
            replyNode.Text = row["TypeName"].ToString();
            replyNode.Value = row["ID"].ToString();
            replyNode.Expanded = false;
            node.ChildNodes.Add(replyNode);
            AddReplies(dt, replyNode);
        }
    }

    /// <summary>
    /// 初始化JS 
    /// </summary>
    private void RegisterJS()
    {
        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.UWGGoodType, "ig_webgrid_an");          //注册 Infragistics js和控件
    }

}

